package cn.itcast.customview.Actrvity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import cn.itcast.customview.Bean.MyBean;
import cn.itcast.customview.R;
import cn.itcast.customview.View.SlideLayout;

public class SlideMenuActivity extends AppCompatActivity {

     private ListView lv_main;
     private ArrayList<MyBean> myBeans;

     private MyAdapter myAdapter;
     private ViewHold viewHold;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slide_menu);

        lv_main=(ListView)findViewById(R.id.lv_main);

        //设置适配器
        //准备数据
        myBeans=new ArrayList<>();


        for (int i=0;i<50;i++){

            myBeans.add(new MyBean("Content"+i));
        }

        myAdapter=new MyAdapter();
       lv_main.setAdapter(myAdapter);
    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return myBeans.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup viewGroup) {

            if (convertView==null){
                convertView=View.inflate(SlideMenuActivity.this,R.layout.item_menu,null);
                viewHold=new ViewHold();
                viewHold.item_content=convertView.findViewById(R.id.item_content);
                viewHold.item_menu=convertView.findViewById(R.id.item_menu);

                convertView.setTag(viewHold);

            }else {
                viewHold= (ViewHold) convertView.getTag();
            }
            //根据位置得到内容
            final MyBean myBean=myBeans.get(position);
            viewHold.item_content.setText(myBean.getName());
            viewHold.item_content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   MyBean myBean1= myBeans.get(position);
                   Toast.makeText(SlideMenuActivity.this,myBean1.getName(),Toast.LENGTH_SHORT).show();
                }
            });
            viewHold.item_menu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    SlideLayout slideLayout=(SlideLayout)view.getParent();
                    slideLayout.cloneMenu();
                    myBeans.remove(myBean);
                    myAdapter.notifyDataSetChanged();
                }
            });

           SlideLayout slideLayout=(SlideLayout) convertView;
           slideLayout.setOnStateChangListenter(new MyOnStateChangListenter());
            return convertView;
        }




    }
    private SlideLayout slideLayout;

    class MyOnStateChangListenter implements SlideLayout.OnStateChangListenter{
        @Override
        public void onClose(SlideLayout layout) {
            if (slideLayout==layout){
                slideLayout=null;

            }

        }

        @Override
        public void onDown(SlideLayout layout) {
            if (slideLayout!=null && slideLayout!=layout){
                slideLayout.cloneMenu();
            }
        }

        @Override
        public void onOpen(SlideLayout layout) {

            slideLayout=layout;

        }
    }
   static class ViewHold {
        TextView item_content;
        TextView item_menu;
    }
}
